package com.fgba.express.controller;


import com.fgba.express.comm.enums.NoticeType;
import com.fgba.express.comm.exception.BizException;
import com.fgba.express.comm.result.ResultCode;
import com.fgba.express.core.controller.BaseController;
import com.fgba.express.data.dto.NoticeDto;
import com.fgba.express.data.entity.Notice;
import com.fgba.express.data.mapstruct.NoticeStruct;
import com.fgba.express.data.search.NoticeSearch;
import com.fgba.express.data.service.INoticeService;
import com.fgba.express.util.BeanContextUtil;
import com.fgba.express.util.CommonUtils;
import com.fgba.express.util.NullChecker;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.time.LocalDateTime;

/**
 * <p>
 * 通知 前端控制器
 * </p>
 *
 * @author fgba
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/notice")
@Api(value = "通知相关操作", tags = "通知相关操作")
public class NoticeController extends BaseController<Notice, NoticeDto, NoticeSearch, INoticeService, NoticeStruct> {
    @Override
    public void createValidation(NoticeDto model) {
        if (NullChecker.isNullOrEmpty(model.getTitle())) {
            log.error("NoticeController -> save: title is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

        if (NullChecker.isNullOrEmpty(model.getContext())) {
            log.error("NoticeController -> save: context is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }
        if (NullChecker.isNull(model.getType()) && NullChecker.isNullOrZero(model.getType().getCode())) {
            log.error("NoticeController -> save: type is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

        if (CommonUtils.equals(model.getType(), NoticeType.PERSONAL) &&NullChecker.isNull(model.getUserIds())) {
            log.error("NoticeController -> save: userIds is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }
    }

    @Override
    public void beforeSave(Notice entity) throws Exception {
        entity.setReleaseName(BeanContextUtil.getBean(Principal.class).getName());
        entity.setReleaseTime(LocalDateTime.now());
    }
}
